package edu.towson.cosc490.incaseofemergency;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import edu.towson.cosc490.incaseofemergency.objects.Constants;
import edu.towson.cosc490.incaseofemergency.objects.SupplyKitData;
import android.os.Bundle;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class ItemDetailEdit extends Activity {
	private TextView itemNameView;
	private EditText amountOwnedEdit;
	private TextView amountRequiredView;
	private CheckBox perishableBox;
	private String itemId;
	private String expirationString;
	private int expirationDateVisibility;
	private LinearLayout expirationLayout;
	private Button updateItemInfo;
	private SupplyKitData supplyDB;
	private String perishable;
	private DatePicker datePicker;
	private Calendar calendarExpiration;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_item_detail_edit);
		initLayout();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_item_detail, menu);
		return true;
	}
	

	
	private void initLayout(){
		expirationDateVisibility = View.VISIBLE;
		itemNameView = (TextView) findViewById(R.id.itemDetailName);
		amountRequiredView = (TextView) findViewById(R.id.textNumberRequired);
		amountOwnedEdit = (EditText)findViewById(R.id.editAmountOwned);
		perishableBox = (CheckBox) findViewById(R.id.checkBoxExpiration);
		expirationLayout = (LinearLayout) findViewById(R.id.layoutExpirationDisplay);
		updateItemInfo = (Button) findViewById(R.id.buttonUpdateItem);
		supplyDB = new SupplyKitData(this);
		perishable = new String();
		datePicker = (DatePicker) this.findViewById(R.id.datePickerExpiration);
		calendarExpiration = Calendar.getInstance();
		Bundle extras = getIntent().getExtras();
		if( extras.get("itemId").equals(null) || extras.get("itemName").equals(null) 
				|| extras.get("itemAmountOwned").equals(null) || extras.get("itemAmountRequired").equals(null)
				|| extras.get("itemPerishable").equals(null) || extras.get("itemExpiration").equals(null) ){
			this.finish();
		} else{
			itemId = extras.getString("itemId");
			itemNameView.setText(extras.getString("itemName"));
			amountRequiredView.setText(extras.getString("itemAmountRequired"));
			amountOwnedEdit.setText(extras.getString("itemAmountOwned"));
			
			if(extras.getString("itemPerishable").equals("true")){
				perishableBox.setChecked(true);
				perishable = "true";
				SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
				try{
					calendarExpiration.setTime(dateFormat.parse(extras.getString("itemExpiration")));
					datePicker.init(calendarExpiration.get(Calendar.YEAR),calendarExpiration.get(Calendar.MONTH), calendarExpiration.get(Calendar.DAY_OF_MONTH), null );
				} catch(ParseException e){

				}
				expirationDateVisibility = View.VISIBLE;
				expirationLayout.setVisibility(expirationDateVisibility);
			} else{
				perishableBox.setChecked(false);
				expirationDateVisibility = View.GONE;
				perishable = "false";
				expirationLayout.setVisibility(expirationDateVisibility);
			}
			
			perishableBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
					if (buttonView.isChecked()){
						expirationDateVisibility = View.VISIBLE;
						perishable = "true";
						expirationLayout.setVisibility(expirationDateVisibility);
					} else{
						expirationDateVisibility = View.GONE;
						perishable = "false";
						expirationLayout.setVisibility(expirationDateVisibility);
					}
				}
			});
			updateItemInfo.setOnClickListener(new OnClickListener(){
				public void onClick(View v){
					updateSupplyKitItem();
				}
			});
			
		}
	}
	
	private void updateSupplyKitItem(){
		SQLiteDatabase db = supplyDB.getWritableDatabase();
		StringBuilder sb = new StringBuilder("");
		if(perishable.equals("true")){
			sb.append(datePicker.getMonth()+1);
			sb.append("/");
			sb.append(datePicker.getDayOfMonth());
			sb.append("/");
			sb.append(datePicker.getYear());
			expirationString = sb.toString();
		} else{
			expirationString = "No Expiration";
		}
		db.execSQL("UPDATE " + Constants.KIT_TABLE_NAME +  " SET " + Constants.AMOUNT_OWNED + "='" +amountOwnedEdit.getText().toString() +"', " 
				+ Constants.PERISHABLE + "='" + perishable + "', " + Constants.EXPIRATON_DATE + "='" + expirationString 
				+ "' WHERE _ID=" + itemId +";" );
		db.close();
		this.finish();
	}
}
